package com.honeycomb.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.honeycomb.common.exception.LoginRedirectException;
import com.honeycomb.common.exception.ServiceException;

public abstract class BaseWebAction extends BaseAction {

	public final String execute() throws Exception {
		String rqURL = this.getRQ().getRequestURI();
		
		String result = null;
		try {
			result = doRequest(getRQ(), getRP(), getSSN());
			return result;
		} catch (Exception ex) {
			logger.fatal("exception -- ", ex);
			if (ex instanceof LoginRedirectException) {
				result = "login";
			} else if (ex instanceof ServiceException) {
				result = "GlobalErrorPage";
				this.getRQ().setAttribute("msg", ex.toString());
			} else {
				this.getRQ().setAttribute("msg", "系统错误，可能原因：" + ex.getMessage());
				result = "GlobalErrorPage";
			}
			logger.fatal("ITOSSAction Exception", ex);
			return result; // 需要配置一个全局错误页面
		} finally {
//			 当msg为空且result 为 "success" 时表示成功，其他为失败
//			logger.fatal(result + ": " + rqURL + " " + this.getRQ().getAttribute("msg"));
//			if (user != null)
//				LogWriter.writeOptLog(user.getO_id(), rqURL, result, (String) this.getRQ().getAttribute("msg"), null, host);

		}
	}

	protected abstract String doRequest(HttpServletRequest request, HttpServletResponse response, HttpSession session);

}
